package edu.gatech.bsbmule.view;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;

/**
 * The GUI that enables players to configure their game
 * 
 * @author Vania, Hanjie, Melissa, Manav
 * @version 1.0 10/6/2013
 * 
 */
public class GameConfig extends JPanel implements IGameConfig {

	//Number of players spinner
	private JSpinner spinnerNumPlayers;
	//Difficulty level comboBox
	private JComboBox<String> comboBoxDifficultyLvl;
	//Type of map comboBox
	private JComboBox<String> comboBoxTypeMap;
	//Number of turns spinner
	private JSpinner spinnerTurns;
	//OK button
	JButton btnOk;

	/**
	 * Create the GUI
	 */
	public GameConfig() {
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JLabel lblMule = new JLabel("M.U.L.E.");
		lblMule.setFont(new Font("Tahoma", Font.PLAIN, 36));
		lblMule.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblMule);

		JSplitPane splitPane0 = new JSplitPane();
		splitPane0.setEnabled(false);
		add(splitPane0);
		splitPane0.setDividerLocation(300);
		JLabel numPlayers = new JLabel("Number of Players:");
		numPlayers.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane0.setLeftComponent(numPlayers);
		spinnerNumPlayers = new JSpinner();
		spinnerNumPlayers.setFont(new Font("Tahoma", Font.PLAIN, 24));
		spinnerNumPlayers.setModel(new SpinnerNumberModel(4, 4, 12, 1));
		splitPane0.setRightComponent(spinnerNumPlayers);

		JSplitPane splitPane1 = new JSplitPane();
		splitPane1.setEnabled(false);
		add(splitPane1);
		splitPane1.setDividerLocation(300);
		JLabel difficultyLvl = new JLabel("Difficulty Level:");
		difficultyLvl.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane1.setLeftComponent(difficultyLvl);

		comboBoxDifficultyLvl = new JComboBox<String>();
		comboBoxDifficultyLvl.setFont(new Font("Tahoma", Font.PLAIN, 24));
		comboBoxDifficultyLvl.setModel(new DefaultComboBoxModel<String>(new String[] {
				"Beginner", "Standard", "Tournament" }));
		splitPane1.setRightComponent(comboBoxDifficultyLvl);

		JSplitPane splitPane2 = new JSplitPane();
		splitPane2.setEnabled(false);
		add(splitPane2);
		splitPane2.setDividerLocation(300);
		JLabel typeMap = new JLabel("Type of Map:");
		typeMap.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane2.setLeftComponent(typeMap);

		comboBoxTypeMap = new JComboBox<String>();
		comboBoxTypeMap.setFont(new Font("Tahoma", Font.PLAIN, 24));
		comboBoxTypeMap.setModel(new DefaultComboBoxModel<String>(new String[] {
				"Standard", "Random" }));
		splitPane2.setRightComponent(comboBoxTypeMap);

		JSplitPane splitPane3 = new JSplitPane();
		splitPane3.setEnabled(false);
		add(splitPane3);
		splitPane3.setDividerLocation(300);
		JLabel turns = new JLabel("Number of Turns:");
		turns.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane3.setLeftComponent(turns);
		spinnerTurns = new JSpinner();
		spinnerTurns.setModel(new SpinnerNumberModel(12, 12, 72, 12));
		spinnerTurns.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane3.setRightComponent(spinnerTurns);

		JSplitPane splitPane4 = new JSplitPane();
		splitPane4.setEnabled(false);
		add(splitPane4);
		splitPane4.setDividerLocation(300);

		btnOk = new JButton("OK");
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane4.setLeftComponent(btnOk);

		JButton btnClear = new JButton("CLEAR");
		btnClear.addMouseListener(new MouseAdapter() {

			@Override
			/**
			 * Clear all the values in number of players, difficulty level, type of map and number of turns
			 * @param A MouseEvent object e
			 */
			public void mouseClicked(MouseEvent e) {
				spinnerNumPlayers.setValue(4);
				comboBoxDifficultyLvl.setSelectedItem("Beginner");
				comboBoxTypeMap.setSelectedItem("Standard");
				spinnerTurns.setValue(12);
			}

		});
		btnClear.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane4.setRightComponent(btnClear);
	}
	
	/**
	 * @see edu.gatech.view.IGameConfig
	 */
	@Override
	public int getNumberOfPlayers() {
		return (Integer) spinnerNumPlayers.getValue();
	}
	
	/**
	 * @see edu.gatech.view.IGameConfig
	 */
	@Override
	public String getDifficultyLevel() {
		return (String) comboBoxDifficultyLvl.getSelectedItem();
	}
	
	/**
	 * @see edu.gatech.view.IGameConfig
	 */
	@Override
	public String getTypeOfMap() {
		return (String) comboBoxTypeMap.getSelectedItem();

	}
	
	/**
	 * @see edu.gatech.view.IGameConfig
	 */
	@Override
	public int getGameTurn() {
		return (Integer) (spinnerTurns.getValue());
	}
	
	/**
	 * @see edu.gatech.view.IGameConfig
	 */
	@Override
	public void setOKButtonListener(MouseAdapter e) {
		btnOk.addMouseListener(e);
	}
}
